package com.llb666.e.shop.api.controller;

import com.llb666.e.shop.api.dto.UserAddressDto;
import com.llb666.e.shop.domain.entity.UserAddress;
import com.llb666.e.shop.domain.service.UserAddressService;
import com.llb666.e.shop.infracore.exception.AppException;
import com.llb666.e.shop.infracore.utils.MobileEmailUtils;
import com.llb666.e.shop.infracore.web.BaseResponse;
import com.llb666.e.shop.infracore.web.CommonError;
import com.llb666.e.shop.infracore.web.DataResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author llb666
 */
@RestController
@Tag(name = "收件人地址相关接口", description = "收件人地址相关接口")
@RequestMapping("/address")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserAddressController {

    private final UserAddressService addressService;

    /**
     * 查询当前登录人的收件地址信息
     *
     * @param userId 用户id
     * @return jsonResult
     */
    @Operation(summary = "根据用户Id查询收获地址", description = "根据用户Id查询收获地址", method = "POST")
    @PostMapping("/list")
    public BaseResponse list(@RequestParam String userId) {
        if (StringUtils.isBlank(userId)) {
            return DataResponse.errorMsg("用户Id不能为空！");
        }
        List<UserAddress> userAddresses = addressService.queryAll(userId);
        return DataResponse.ok(userAddresses);
    }

    @Operation(summary = "用户新增收获地址", description = "用户新增收获地址", method = "POST")
    @PostMapping("/add")
    public BaseResponse list(@RequestBody UserAddressDto addressDto) {
        checkAddress(addressDto);
        addressService.addUserAddress(UserAddressDto.Converter.INSTANCE.dto2do(addressDto));
        return BaseResponse.ok();
    }

    @Operation(summary = "用户修改地址", description = "用户修改地址", method = "POST")
    @PostMapping("/update")
    public BaseResponse update(@RequestBody UserAddressDto addressDto) {
        if (StringUtils.isBlank(addressDto.getAddressId())) {
            return DataResponse.errorMsg("修改地址错误：addressId不能为空");
        }
        checkAddress(addressDto);
        addressService.updateUserAddress(UserAddressDto.Converter.INSTANCE.dto2do(addressDto));
        return BaseResponse.ok();
    }

    @Operation(summary = "用户删除地址", description = "用户删除地址", method = "POST")
    @PostMapping("/delete")
    public BaseResponse delete(
            @RequestParam String userId,
            @RequestParam String addressId) {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {
            return DataResponse.errorMsg("删除地址错误：addressId不能为空");
        }
        addressService.deleteUserAddress(userId, addressId);
        return DataResponse.ok();
    }

    @Operation(summary = "用户设置默认地址", description = "用户设置默认地址", method = "POST")
    @PostMapping("/setDefault")
    public BaseResponse setDefault(
            @RequestParam String userId,
            @RequestParam String addressId) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {
            return DataResponse.errorMsg("用户或者地址Id不能为空.");
        }
        addressService.updateUserAddressToBeDefault(userId, addressId);
        return BaseResponse.ok();
    }

    /**
     * 收获地址表单校验.
     *
     * @param addressDto 表单提交的收获地址dto
     */
    private void checkAddress(UserAddressDto addressDto) {
        String receiver = addressDto.getReceiver();
        if (StringUtils.isBlank(receiver)) {
            throw new AppException(CommonError.RequestParamsInvalid, "收货人不能为空");
        }
        if (receiver.length() > 12) {
            throw new AppException(CommonError.RequestParamsInvalid, "收货人姓名不能太长");
        }
        String mobile = addressDto.getMobile();
        if (StringUtils.isBlank(mobile)) {
            throw new AppException(CommonError.RequestParamsInvalid, "收货人手机号不能为空");
        }
        if (mobile.length() != 11) {
            throw new AppException(CommonError.RequestParamsInvalid, "收货人手机号长度不正确");
        }
        boolean isMobileOk = MobileEmailUtils.checkMobileIsOk(mobile);
        if (!isMobileOk) {
            throw new AppException(CommonError.RequestParamsInvalid, "收货人手机号格式不正确");
        }
        String province = addressDto.getProvince();
        String city = addressDto.getCity();
        String district = addressDto.getDistrict();
        String detail = addressDto.getDetail();
        if (StringUtils.isBlank(province) || StringUtils.isBlank(city)
                || StringUtils.isBlank(district) || StringUtils.isBlank(detail)) {
            throw new AppException(CommonError.RequestParamsInvalid, "收货地址信息不能为空");
        }
    }

}
